package com.gkoudai.open.platform.common;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.gkoudai.open.platform.entry.IndicatorData;
import com.gkoudai.open.platform.entry.IndicatorInfo;
import com.gkoudai.open.platform.entry.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author wangqi
 * @date 2025-02-19 15:14
 */
public class ResponseHandlerUtils {

    public static ResponseBody<List<IndicatorInfo>> parseIndicatorResponse(String response) {
        ResponseBody<List<IndicatorInfo>> responseBody = ResponseBody.create();
        JSONObject responseObject = JSON.parseObject(response);
        Integer status = responseObject.getInteger("status");
        String desc = responseObject.getString("desc");
        responseBody.setStatus(status);
        responseBody.setDesc(desc);
        JSONObject dataObject = responseObject.getJSONObject("data");
        if (Objects.isNull(dataObject)) {
            responseBody.setData(null);
            return responseBody;
        }
        List<JSONObject> list = dataObject.getJSONObject("result").getJSONArray("ts").toList(JSONObject.class);
        if (list.isEmpty()) {
            responseBody.setData(Collections.emptyList());
            return responseBody;
        }
        List<IndicatorInfo> dataList = new ArrayList<>();
        for (JSONObject jsonObject : list) {
            IndicatorInfo indicatorInfo = new IndicatorInfo();
            JSONArray t = jsonObject.getJSONArray("t");
            if (t==null||t.isEmpty()){
                continue;
            }
            JSONObject data = t.toList(JSONObject.class).get(0);
            String indexName = data.getString("v1");
            String indexCode = data.getString("v2");
            String unit = data.getString("v3");
            String source = data.getString("v4");
            String period = data.getString("v5");
            Integer count = jsonObject.getInteger("c");
            JSONArray valueList = data.getJSONArray("vs");
            JSONArray dateList = jsonObject.getJSONArray("ts");
            List<IndicatorData> indicatorDataList = new ArrayList<>();
            for (int i = 0; i < valueList.size(); i++) {
                String value = valueList.getString(i);
                String date = dateList.getString(i);
                IndicatorData indicatorData = new IndicatorData();
                indicatorData.setDate(date);
                indicatorData.setValue(value);
                indicatorDataList.add(indicatorData);
            }
            indicatorInfo.setCount(count);
            indicatorInfo.setIndexName(indexName);
            indicatorInfo.setIndexCode(indexCode);
            indicatorInfo.setUnit(unit);
            indicatorInfo.setSource(source);
            indicatorInfo.setPeriod(period);
            indicatorInfo.setDataList(indicatorDataList);
            dataList.add(indicatorInfo);
        }
        responseBody.setData(dataList);
        return responseBody;

    }
}
